package com.android.takeaway.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

import com.android.takeaway.activity.FoodActivity;
import com.android.takeaway.R;
import com.android.takeaway.bean.Food;

/**
 * 菜单适配器
 */
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MyViewHolder> {
    //食品列表
    private List<Food> foodBeanList;
    //上下文参数
    private Context context;

    private OnAddButtonClickListener onAddButtonClickListener;

    //设置数据
    public void setData(List<Food> foodBeanList) {
        this.foodBeanList = foodBeanList;
        notifyDataSetChanged(); //让界面重新刷新
    }

    public void setOnAddButtonClickListener(OnAddButtonClickListener onAddButtonClickListener) {
        this.onAddButtonClickListener = onAddButtonClickListener;
    }

    //构造方法
    public MenuAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //view对象
        View view = LayoutInflater.from(context).inflate(R.layout.menu_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view); //生成holder
        return holder;
    }

    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Food foodBean = foodBeanList.get(position);
        // 获取图片的资源引用
        int resourceId = context.getResources().getIdentifier(foodBean.getFoodPic(), "mipmap", context.getPackageName());
        // 加载食物图片到holder
        Glide.with(context).load(resourceId).into(holder.iv_foodPic);

        holder.tv_foodName.setText(foodBean.getFoodName());          //显示食物名称
        holder.tv_taste.setText(foodBean.getTaste());                //显示食物口味
        holder.tv_saleNum.setText("销量： " + foodBean.getSaleNum()); //显示食物销量
        holder.tv_price.setText("￥：" + foodBean.getPrice());        //显示食物价格

        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddButtonClickListener.OnAddButtonClick(foodBean); //调用接口对象的方法


            }
        });
        //图片添加事件：打开食品详情页面
        holder.iv_foodPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开商品详情Activity
                Intent intent = new Intent(context, FoodActivity.class);
                intent.putExtra("foodBean", foodBean); //传参数
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        //判断list里面是否为空，如果是空则返回0否则返回本身长度
        return foodBeanList == null ? 0 : foodBeanList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_foodPic;
        TextView tv_foodName, tv_taste, tv_saleNum, tv_price, tv_add;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_foodPic = itemView.findViewById(R.id.iv_foodPic);
            tv_foodName = itemView.findViewById(R.id.tv_foodName);
            tv_taste = itemView.findViewById(R.id.tv_taste);
            tv_saleNum = itemView.findViewById(R.id.tv_saleNum);
            tv_price = itemView.findViewById(R.id.tv_foodCost);
            tv_add = itemView.findViewById(R.id.tv_add);

        }
    }

    //自定义接口（自定义事件）
    public interface OnAddButtonClickListener {
        public void OnAddButtonClick(Food foodBean);
    }
}
